#include "ContraStateOnLand.h"




void ContraStateOnLand::stand()
{
	if (_data->checkIsFlying() && _data->canInteract) {
		return;
	}
	if ((_data->getSprites()->checkState(_STATE_CONTRA_FIRE_SIT_LEFT) || _data->getSprites()->checkState(_STATE_CONTRA_FIRE_SIT_RIGHT)) && !_data->getSprites()->currentSprite->endOfSheet()) return;
	// check current state to set stand left or right
	if (_data->getSprites()->state % 2 == 0) {
		// set current state to stand left sprites
		if (!(_data->bodySprites->state > _STATE_CONTRA_MOVE_LEFT && !_data->bodySprites->currentSprite->endOfSheet())) {
			_data->bodySprites->setState(_STATE_CONTRA_STAND_LEFT);
			_data->sprites->setState(_STATE_CONTRA_STAND_LEFT);
		}
	}
	else {
		// set current sprite to stand right sprite
		if (!(_data->bodySprites->state > _STATE_CONTRA_MOVE_RIGHT && !_data->bodySprites->currentSprite->endOfSheet())) {
			_data->bodySprites->setState(_STATE_CONTRA_STAND_RIGHT);
			_data->sprites->setState(_STATE_CONTRA_STAND_RIGHT);
		}
	}
}

void ContraStateOnLand::standUp()
{
	// Check is flying, if flying not use sprite stand
	if (_data->checkIsFlying()) {
		return;
	}

	if (_data->checkIsMoving()) {
		if (_data->getSprites()->state % 2 == 0) {
			_data->bodySprites->setState(_STATE_CONTRA_UP_CROSS_LEFT);
			_data->sprites->setState(_STATE_CONTRA_MOVE_LEFT);
		}
		else {
			_data->bodySprites->setState(_STATE_CONTRA_UP_CROSS_RIGHT);
			_data->sprites->setState(_STATE_CONTRA_MOVE_RIGHT);
		}
	}
	else {
		// check current state to set stand left or right
		if (_data->getSprites()->state % 2 == 0) {
			if (!(_data->bodySprites->state == _STATE_CONTRA_FIRE_UP_LEFT && !_data->bodySprites->currentSprite->endOfSheet())) {
				_data->bodySprites->setState(_STATE_CONTRA_UP_LEFT);

			}
		}
		else {
			if (!(_data->bodySprites->state == _STATE_CONTRA_FIRE_UP_RIGHT && !_data->bodySprites->currentSprite->endOfSheet()))
				_data->bodySprites->setState(_STATE_CONTRA_UP_RIGHT);

		}
	}
	_data->rect->Height = 75;
}

void ContraStateOnLand::moveRight()
{
	int bodyState = _data->getBodySprites()->state;
	// if contra is not flying
	if (!_data->checkIsFlying()) {
		if (!(_data->bodySprites->state == _STATE_CONTRA_FIRE_RIGHT  && _data->timeDelayShooting > 0)) {
			if (!(_data->bodySprites->state > _STATE_CONTRA_MOVE_LEFT && !_data->bodySprites->currentSprite->endOfSheet())) {
				_data->bodySprites->setState(_STATE_CONTRA_MOVE_RIGHT);
				_data->sprites->setState(_STATE_CONTRA_MOVE_RIGHT);
				if (_data->sprites->currentSprite->currentIndex() == 0)
					_data->sprites->currentSprite->setIndex(1);
			}
		}
	}
	else { // if contra is flying, make contra fly to right
		flyRight();
	}
}

void ContraStateOnLand::moveLeft()
{
	int bodyState = _data->getBodySprites()->state;
	// if contra not flying
	if (!_data->checkIsFlying()) {
		// Check if is current shooting left, not load sprite move left
		if (!(_data->bodySprites->state == _STATE_CONTRA_FIRE_LEFT  && _data->timeDelayShooting > 0)) {
			if (!(_data->bodySprites->state > _STATE_CONTRA_MOVE_LEFT && !_data->bodySprites->currentSprite->endOfSheet())) {				_data->bodySprites->setState(_STATE_CONTRA_MOVE_LEFT);
			_data->sprites->setState(_STATE_CONTRA_MOVE_LEFT);
			_data->bodySprites->setState(_STATE_CONTRA_MOVE_LEFT);
			if (_data->sprites->currentSprite->currentIndex() == 0)
				_data->sprites->currentSprite->setIndex(1);
			}
		}
	}
	else { // if contra is flying, make contra fly to right
		flyLeft();
	}
}

void ContraStateOnLand::fire()
{
	// Check is flying
	// Check is flying, if flying not use sprite stand
	//if (_data->checkIsFlying()) {
	//	return;
	//}
	// init body state 
	int bodyState = _data->getBodySprites()->state;
	if (!_data->checkDelayShooting()) {
		// check contra are standing
		if (bodyState <= 6) {
			// reset timeDelayShooting
			_data->resetTimeDelayShooting();
			if (_data->getSprites()->state % 2 == 0) {
				if (!_data->checkIsFlying()){
					_data->bodySprites->setState(_STATE_CONTRA_FIRE_LEFT);
				}
				_data->bulletContainer->fire(_data->rect->getLeft() + 5, _data->rect->Y + 9, _BULLET_DIRECTION_LEFT, NULL);
			}
			else {
				if (!_data->checkIsFlying()) _data->bodySprites->setState(_STATE_CONTRA_FIRE_RIGHT);
				_data->bulletContainer->fire(_data->rect->getRight() - 5, _data->rect->Y + 9, _BULLET_DIRECTION_RIGHT, NULL);
			}
		}
		// check contra fire up
		else if ((bodyState >= 9) && (bodyState <= 10)) {
			fireUp();
		}
		// check contra fire cross up
		else if ((bodyState == _STATE_CONTRA_UP_CROSS_RIGHT) || (bodyState == _STATE_CONTRA_UP_CROSS_LEFT)) {
			fireCrossUp();

		}
		// check contra fire cros down
		else if ((bodyState == _STATE_CONTRA_DOWN_CROSS_RIGHT) || (bodyState == _STATE_CONTRA_DOWN_CROSS_LEFT)) {
			fireCrossDown();
		}
		// check contra sit fire
		else if ((bodyState == _STATE_CONTRA_SIT_RIGHT) || (bodyState == _STATE_CONTRA_SIT_LEFT)) {
			fireSit();
		} 
		else if ((bodyState == _STATE_CONTRA_FLY_LEFT) || (bodyState == _STATE_CONTRA_FLY_RIGHT)) {
			fireOnFly();
		}
	}
}

void ContraStateOnLand::fireSit()
{
	if (_data->getSprites()->state % 2 == 0) {
		_data->bodySprites->setState(_STATE_CONTRA_FIRE_SIT_LEFT);
		_data->sprites->setState(_STATE_CONTRA_FIRE_SIT_LEFT);
		_data->bulletContainer->fire(_data->rect->getLeft() - 10, _data->rect->getBottom() + 20, _BULLET_DIRECTION_LEFT, NULL);
	}
	else {
		_data->bodySprites->setState(_STATE_CONTRA_FIRE_SIT_RIGHT);
		_data->sprites->setState(_STATE_CONTRA_FIRE_SIT_RIGHT);
		_data->bulletContainer->fire(_data->rect->getRight() + 10, _data->rect->getBottom() + 20, _BULLET_DIRECTION_RIGHT, NULL);
	}
}

void ContraStateOnLand::fireOnFly()
{
	if (_data->getSprites()->state % 2 == 0) {
		_data->bulletContainer->fire(_data->rect->getLeft() + 5, _data->rect->Y + 9, _BULLET_DIRECTION_LEFT, NULL);
	}
	else {
		_data->bulletContainer->fire(_data->rect->getRight() - 5, _data->rect->Y + 9, _BULLET_DIRECTION_RIGHT, NULL);
	}
}

void ContraStateOnLand::fireCrossDown()
{
	//if (!_data->checkDelayShooting()) {
	if (_data->getSprites()->state % 2 == 0) {
		_data->bodySprites->setState(_STATE_CONTRA_FIRE_DOWN_CROSS_LEFT);
		_data->sprites->setState(_STATE_CONTRA_MOVE_LEFT);
		_data->bulletContainer->fire(_data->rect->X - 20, _data->rect->getBottom() + 20, _BULLET_DIRECTION_DOWN_LEFT, NULL);
	}
	else {
		_data->bodySprites->setState(_STATE_CONTRA_FIRE_DOWN_CROSS_RIGHT);
		_data->bodySprites->setState(_STATE_CONTRA_MOVE_RIGHT);
		_data->bulletContainer->fire(_data->rect->X + 20, _data->rect->getBottom() + 20, _BULLET_DIRECTION_DOWN_RIGHT, NULL);
	}
	//}
}

void ContraStateOnLand::fireCrossUp()
{
	//if (!_data->checkDelayShooting()) {
	if (_data->getSprites()->state % 2 == 0) {
		_data->bodySprites->setState(_STATE_CONTRA_FIRE_UP_CROSS_LEFT);
		_data->sprites->setState(_STATE_CONTRA_MOVE_LEFT);
		_data->bulletContainer->fire(_data->rect->X - 20, _data->rect->getTop() + 10, _BULLET_DIRECTION_UP_LEFT, NULL);
	}
	else {
		_data->bodySprites->setState(_STATE_CONTRA_FIRE_UP_CROSS_RIGHT);
		_data->sprites->setState(_STATE_CONTRA_MOVE_RIGHT);
		_data->bulletContainer->fire(_data->rect->X + 20, _data->rect->getTop() + 10, _BULLET_DIRECTION_UP_RIGHT, NULL);
	}
	//}
}

void ContraStateOnLand::fireUp()
{
	//if (!_data->checkDelayShooting()) {
	if (_data->getSprites()->state % 2 == 0) {
		_data->bodySprites->setState(_STATE_CONTRA_FIRE_UP_LEFT);
		_data->bulletContainer->fire(_data->rect->X - 5, _data->rect->getTop() + 20, _BULLET_DIRECTION_UP, NULL);
	}
	else {
		_data->bodySprites->setState(_STATE_CONTRA_FIRE_UP_RIGHT);
		_data->bulletContainer->fire(_data->rect->X + 5, _data->rect->getTop() + 20, _BULLET_DIRECTION_UP, NULL);
	}
	//}
}

void ContraStateOnLand::sit()
{
	// Check is flying, if flying not use sprite stand
	if (_data->checkIsFlying()) {
		return;
	}
	// Check moving
	if (_data->checkIsMoving()) {

		if (_data->getSprites()->state % 2 == 0) {
			_data->bodySprites->setState(_STATE_CONTRA_DOWN_CROSS_LEFT);
			_data->sprites->setState(_STATE_CONTRA_MOVE_LEFT);
		}
		else {
			_data->bodySprites->setState(_STATE_CONTRA_DOWN_CROSS_RIGHT);
			_data->sprites->setState(_STATE_CONTRA_MOVE_RIGHT);
		}
	}
	else {
		// check contra in left
		if ((_data->bodySprites->checkState(_STATE_CONTRA_FIRE_SIT_RIGHT) || _data->bodySprites->checkState(_STATE_CONTRA_FIRE_SIT_LEFT)) && !_data->bodySprites->currentSprite->endOfSheet()) return;
		if (_data->sprites->state % 2 == 0) {
			_data->sprites->setState(_STATE_CONTRA_SIT_LEFT);
			_data->bodySprites->setState(_STATE_CONTRA_SIT_LEFT);
		}
		else {
			_data->sprites->setState(_STATE_CONTRA_SIT_RIGHT);
			_data->bodySprites->setState(_STATE_CONTRA_SIT_RIGHT);
		}
	}
}

void ContraStateOnLand::draw(long time)
{
	int alpha = 255;
	if (_data->undeadDelay > 0) {
		if (time % 5 == 0) isAlpha = !isAlpha;
		if (isAlpha) alpha = 150;
		else alpha = 220;
		_data->undeadDelay--;
	}
	_data->sprites->draw(alpha);
	_data->bodySprites->draw(alpha); 

	_data->bulletContainer->draw(0);
}

void ContraStateOnLand::update()
{
	//if (_data->jumpingTimeCounter > 0) fly();
	_data->sprites->updateCurrent();
	_data->bodySprites->updateCurrent();

	_data->bulletContainer->update(0);

	if (_data->timeDelayShooting > 0) _data->timeDelayShooting--;
}

void ContraStateOnLand::fly()
{
	if (_data->jumpingTimeCounter > 0) {
		// check current state to set stand left or right
		if (_data->canInteract) {
			if (_data->getSprites()->state % 2 == 0) {
				flyLeft();
			}
			else {
				flyRight();
			}
		} else {
			stand();
		}
	}
}

void ContraStateOnLand::flyLeft()
{
	// set current state to stand left sprites
	_data->sprites->setState(_STATE_CONTRA_FLY_LEFT);
	_data->bodySprites->setState(_STATE_CONTRA_FLY_LEFT);
	/*if (!_data->checkCorrectState()) {
	if (!(_data->sprites->currentSprite->currentIndex() > _data->bodySprites->currentSprite->currentIndex())) {
	_data->sprites->currentSprite->setIndex(_data->bodySprites->currentSprite->currentIndex());
	} else {
	_data->bodySprites->currentSprite->setIndex(_data->sprites->currentSprite->currentIndex());
	}
	}*/
}

void ContraStateOnLand::flyRight()
{
	// set current sprite to stand right sprite
	_data->sprites->setState(_STATE_CONTRA_FLY_RIGHT);
	_data->bodySprites->setState(_STATE_CONTRA_FLY_RIGHT);
	/*if (!_data->checkCorrectState()) {
	if (!(_data->sprites->currentSprite->currentIndex() > _data->bodySprites->currentSprite->currentIndex())) {
	_data->sprites->currentSprite->setIndex(_data->bodySprites->currentSprite->currentIndex());
	} else {
	_data->bodySprites->currentSprite->setIndex(_data->sprites->currentSprite->currentIndex());
	}
	}*/
}

void ContraStateOnLand::firstOnLand()
{
	if (_data->bodySprites->state == _STATE_CONTRA_FLY_LEFT || _data->bodySprites->state == _STATE_CONTRA_FLY_RIGHT) {
		_data->bodySprites->currentSprite->setToEnd();
		_data->sprites->currentSprite->setToEnd();
	}
}
